﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Web;
using Top.Data;

namespace Top.PointStore.Business
{
    /*功能说明:兑换规则操作管理类
    *版本:
    *参与人:王士锋
    *修改日期:2015.3.4
    */
    /// <summary>
    /// 兑换规则
    /// </summary>
    public static class ExchangeRuleManager
    {
        /// <summary>增加一条数据
        ///<para>0:更新失败</para>
        ///<para>-1:商品id有误</para>
        ///<para>-2:兑换积分不能小于0</para>
        ///<para>-3:开始时间有误</para>
        ///<para>-4:结束时间有误</para>
        ///<para>-5:用户兑换次数不能小于0</para>
        ///<para>-6:每日兑换数量不能小于0</para>
        ///<para>-7:每日兑换开始时间不能为空</para>
        ///<para>-8:每日兑换结束时间不能为空</para>
        ///<para>更新成功返回id</para>
        /// </summary>
        public static async Task<int> Add(T_ExchangeRuleExpand model)
        {
            if (model.F_ProductKid < 0)
            {
                return -1;
            }
            if (model.F_BeginTime > model.F_EndTime)
            {
                var beginTemp = model.F_BeginTime;
                model.F_BeginTime = model.F_EndTime;
                model.F_EndTime = beginTemp;
            }
            if (model.F_PointNum < 0)
            {
                return -2;
            }
            if (model.F_BeginTime == null)
            {
                return -3;
            }
            if (model.F_EndTime == null)
            {
                return -4;
            }
            if (model.F_UserExchangeNum < 0)
            {
                return -5;
            }
            if (model.F_IsExchange == 1)
            {
                if (model.F_ExchangeNum < 0)
                {
                    return -6;
                }
                if (string.IsNullOrEmpty(model.F_ExchangeBeginTime))
                {
                    return -7;
                }
                if (string.IsNullOrEmpty(model.F_ExchangeEndTime))
                {
                    return -8;
                }
            }

            const string sql = @"insert into T_ExchangeRule (F_ProductKid,F_BeginTime,F_EndTime,F_PointNum,F_Disabled,F_CreatedTime
,F_UpdatedTime,F_Status,F_ExchangeType,F_UserExchangeNum,F_ExchangeNum,F_ExchangeBeginTime,F_ExchangeEndTime,F_IsExchange,F_IsDelete,F_IsMeskill) 
                values ({0:F_ProductKid},
                        {0:F_BeginTime},
                        {0:F_EndTime},
                        {0:F_PointNum},
                        {0:F_Disabled},
                        now(),
                        now(),
                        {0:F_Status},
                        {0:F_ExchangeType},
                        {0:F_UserExchangeNum},
                        {0:F_ExchangeNum},
                        {0:F_ExchangeBeginTime},
                        {0:F_ExchangeEndTime},
                        {0:F_IsExchange},
                        {0:F_IsDelete},
                        {0:F_IsMeskill});
                select LAST_INSERT_ID()";
            using (var db = new DbTour())
            {
                //if (model.F_Disabled == 1) {
                //    UpdateField(model.F_ProductKid);
                //}

                int ruleId = await db.Sql(sql, model).ExecuteScalar<int>();

                //插入规则与城市
                if (!string.IsNullOrEmpty(model.RuleToCity))
                {
                    var cityList = model.RuleToCity.Split(',');
                    for (int i = 0; i < cityList.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(cityList[i]))
                            await RuleToCityManager.Add(new T_RuleToCity { F_RuleKid = ruleId, F_CityKid = int.Parse(cityList[i]) });
                    }

                }
                return ruleId;
            }
        }

        /// <summary> 更新一条数据
        ///<para>0:更新失败</para>
        ///<para>-1:商品id有误</para>
        ///<para>-2:兑换积分不能小于0</para>
        ///<para>-3:开始时间有误</para>
        ///<para>-4:结束时间有误</para>
        ///<para>-5:用户兑换次数不能小于0</para>
        ///<para>-6:每日兑换数量不能小于0</para>
        ///<para>-7:每日兑换开始时间不能为空</para>
        ///<para>-8:每日兑换结束时间不能为空</para>
        ///<para>-9:兑换规则id有误</para>
        /// <para>更新成功返回受影响行数</para>
        /// </summary>
        public static async Task<int> Update(T_ExchangeRuleExpand model)
        {
            if (model.F_ProductKid <= 0)
            {
                return -1;
            }
            if (model.F_BeginTime > model.F_EndTime)
            {
                var beginTemp = model.F_BeginTime;
                model.F_BeginTime = model.F_EndTime;
                model.F_EndTime = beginTemp;
            }
            if (model.F_PointNum < 0)
            {
                return -2;
            }
            if (model.F_BeginTime == null)
            {
                return -3;
            }
            if (model.F_EndTime == null)
            {
                return -4;
            }
            if (model.F_UserExchangeNum < 0)
            {
                return -5;
            }
            if (model.F_IsExchange == 1)
            {
                if (model.F_ExchangeNum < 0)
                {
                    return -6;
                }
                if (string.IsNullOrEmpty(model.F_ExchangeBeginTime))
                {
                    return -7;
                }
                if (string.IsNullOrEmpty(model.F_ExchangeEndTime))
                {
                    return -8;
                }
            }
            if (model.Kid <= 0)
            {
                return -9;
            }
            const string sql = @"update T_ExchangeRule set 
                F_ProductKid={0:F_ProductKid},
                F_BeginTime={0:F_BeginTime},
                F_EndTime={0:F_EndTime},
                F_PointNum={0:F_PointNum},
                F_Disabled={0:F_Disabled},
                F_UpdatedTime=now(),
                F_Status={0:F_Status},
                F_ExchangeType={0:F_ExchangeType},
                F_UserExchangeNum={0:F_UserExchangeNum},
                F_ExchangeNum={0:F_ExchangeNum},
                F_ExchangeBeginTime={0:F_ExchangeBeginTime},
                F_ExchangeEndTime={0:F_ExchangeEndTime},
                F_IsExchange={0:F_IsExchange},
                F_IsMeskill={0:F_IsMeskill}
              where Kid={0:Kid}";

            using (var db = new DbTour())
            {
                return await db.Sql(sql, model).ExecuteNonQuery();
            }
        }

        /// <summary>根据编号得到一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<T_ExchangeRule> GetModelByKid(int kid)
        {
            if (kid <= 0) return null;
            const string sql = @"select * from T_ExchangeRule where Kid = {0} limit 1";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kid).FirstOrDefault<T_ExchangeRule>();
            }
        }

        /// <summary>根据产品编号得到一条启用规则记录
        /// </summary>
        /// <param name="productKid"></param>
        /// <returns></returns>
        public static async Task<T_ExchangeRule> GetModelByProductKid(int productKid)
        {
            if (productKid < 0) return null;
            const string sql = @"select * from T_ExchangeRule where F_ProductKid = {0} and F_Disabled = 1 limit 1";
            using (var db = new DbTour())
            {
                var result = await db.Sql(sql, productKid).FirstOrDefault<T_ExchangeRule>();

                //获取城市
                const string sql2 = @"select * from T_RuleToCity where F_RuleKid = {0}";
                result.T_RuleToCity = await db.Sql(sql2, result.Kid).ToList<T_RuleToCity>();

                return result;
            }
        }

        /// <summary>根据产品编号获取规则列表
        /// </summary>
        /// <param name="productKid"></param>
        /// <returns></returns>
        public static async Task<List<T_ExchangeRule>> GetListByProductKid(int productKid)
        {
            if (productKid <= 0) return null;
            const string sql = @"select * from T_ExchangeRule where F_ProductKid = {0} limit 1";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, productKid).ToList<T_ExchangeRule>();
            }
        }

        /// <summary>更新状态字段
        /// </summary>
        /// <param name="productKid"></param>
        /// <returns></returns>
        public static async Task<int> UpdateField(int productKid)
        {
            if (productKid <= 0) return -1;
            const string sql = @"update T_ExchangeRule set F_Disabled=0 where F_ProductKid={0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, productKid).ExecuteNonQuery();
            }
        }

    }
}
